package cn.iocoder.yudao.hires.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.hires.entity.DictCityDO;
import cn.iocoder.yudao.hires.entity.DictIndustryDO;
import cn.iocoder.yudao.hires.service.DictIndustryService;
import cn.iocoder.yudao.hires.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 行业字典")
@RestController
@RequestMapping("/resume/dict-industry")
@Validated
public class DictIndustryController {

    @Resource
    private DictIndustryService dictIndustryService;

    @PostMapping("/create")
    @Operation(summary = "创建行业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Integer> createDictIndustry(@Valid @RequestBody DictIndustrySaveReqVO createReqVO) {
        return success(dictIndustryService.createDictIndustry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新行业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> updateDictIndustry(@Valid @RequestBody DictIndustrySaveReqVO updateReqVO) {
        dictIndustryService.updateDictIndustry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除行业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> deleteDictIndustry(@RequestParam("id") Integer id) {
        dictIndustryService.deleteDictIndustry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得行业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<DictIndustryRespVO> getDictIndustry(@RequestParam("id") Integer id) {
        DictIndustryDO dictIndustry = dictIndustryService.getDictIndustry(id);
        return success(BeanUtils.toBean(dictIndustry, DictIndustryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得行业字典分页")
    @TenantIgnore
    @PermitAll
    public CommonResult<PageResult<DictIndustryRespVO>> getDictIndustryPage(@Valid DictIndustryPageReqVO pageReqVO) {
        PageResult<DictIndustryDO> pageResult = dictIndustryService.getDictIndustryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictIndustryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出行业字典 Excel")
    @TenantIgnore
    @PermitAll
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictIndustryExcel(@Valid DictIndustryPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictIndustryDO> list = dictIndustryService.getDictIndustryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "行业字典.xls", "数据", DictIndustryRespVO.class, BeanUtils.toBean(list, DictIndustryRespVO.class));
    }


    @TenantIgnore
    @PermitAll
    @GetMapping("/tree")
    @Operation(summary = "行业字典 树形结构")
    public CommonResult<List<DictIndustryTreeNodeVO>> getCategoryTree() {
        List<DictIndustryDO> categories = dictIndustryService.getList();
        List<DictIndustryTreeNodeVO> tree = dictIndustryService.buildCategoryTree(categories);
        return success(tree);
    }

}